package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import org.mobiletrain.www.secondproject2.R;

import java.util.ArrayList;

import Utils.ImageUtils;
import bean.TuiJian;

/**
 * Created by aaa on 15-4-28.
 */
public class TuiJianAdapter extends BaseAdapter{
    private ArrayList<TuiJian> data;
    private Context context;
    private LayoutInflater inflater;

    public TuiJianAdapter(ArrayList<TuiJian> data, Context context) {
        this.data = data;
        this.context = context;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (data!=null){
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView=inflater.inflate(R.layout.tuijianlayout,null);
            holder=new ViewHolder();
            holder.placeIcon= (ImageView) convertView.findViewById(R.id.place_icon);
            holder.placeName= (TextView) convertView.findViewById(R.id.place_name);
            holder.placeMain= (TextView) convertView.findViewById(R.id.place_main);
            holder.currPrice= (TextView) convertView.findViewById(R.id.place_currprice);
            holder.origePrice= (TextView) convertView.findViewById(R.id.place_origeprice);
            convertView.setTag(holder);

        }else {
            holder= (ViewHolder) convertView.getTag();
        }

        TuiJian tuiJian = data.get(position);
        holder.placeIcon.setImageResource(R.drawable.ic_launcher);
        BitmapUtils bitmapUtils = ImageUtils.getBitmapUtils(context);
        bitmapUtils.display(holder.placeIcon,"http://cdn5.yaochufa.com/"+tuiJian.getmImageUrl());
        holder.placeName.setText(tuiJian.getProductName());
        holder.placeMain.setText(tuiJian.getProductDescription());
        holder.currPrice.setText(tuiJian.getPrice()+"      |   ");
        holder.origePrice.setText(tuiJian.getOriginalPrice());

        return convertView;
    }
    class ViewHolder{
        ImageView placeIcon;
        TextView placeName;
        TextView placeMain;
        TextView currPrice;
        TextView origePrice;

    }
}
